/**
 * package: services
 * author: zhaiyujin
 * description:
 */
package services

import (
	"errors"
	"github.com/kataras/iris/v12"
	"huanteng/vendors/crypto/gbcrypt"
	"huanteng/vendors/paginate"
	"huanteng/vendors/validator"
	"huanteng/web/global/application"
	"huanteng/web/middleware"
	"huanteng/web/models"
	"huanteng/web/repositories"
	"huanteng/web/request"
	"reflect"
)

const guard string ="admins"
/*type AdminService interface {
	Login(ctx iris.Context)(m map[string]interface{} ,err error)
	Create(ctx iris.Context) error //新增
	AdminList(ctx iris.Context) //获取列表
	Delete(id uint) //删除
	Update(id uint,ctx iris.Context)(err error) //更新

}*/

type AdminService struct {
	repo  *repositories.AdminRepository
	saveAdmin *request.SaveAdmin
}


func (a *AdminService) Delete(ctx iris.Context) error {
	 var ids []uint
	 err:=ctx.ReadJSON(&ids)
	 if err!=nil{
	 	return err
	 }
	a.repo.Delete(ids)
	 return nil
}

func (a *AdminService) GetById(id uint) models.Admin {
	data:= a.repo.FindById(id)
	data.Pwd=""
	return data
}

func (a *AdminService)  GetRoleById(id uint) []uint{
	return a.repo.FindRoleById(id)
}

func (a *AdminService)Update(ctx iris.Context)(err error) {
	user:= models.Admin{}
    err=a.saveAdmin.Validate(ctx,&user)
	if err!=nil{
		return
	}
	if user.Pwd!=""{
		user.Pwd,_=gbcrypt.GeneratePassWd(user.Pwd)
	}

	a.repo.Update(&user)
	return nil
}
func (a *AdminService)AdminList(ctx iris.Context) {
	model:=[]models.Admin{}
	 a.repo.AdminList(ctx,&model)
	if model!=nil{
		//采用range 不能修改结构体数组
	for i:=0;i<len(model);i++{
		model[i].Pwd=""
	}
	paginate.Page["data"]=model

	}


}
func (a *AdminService)Create(ctx iris.Context) (models.Admin, error)  {
	model:=models.Admin{}
	err:=validator.ValidateInfo(ctx.ReadJSON(&model))
	if err!=nil{
		return model,err
	}
	admin:=a.repo.FindByName(model.Name)
     if admin.ID!=0{
     err=errors.New("管理员已经存在了");
     	return model,err
	 }

	model.Pwd,_= gbcrypt.GeneratePassWd(model.Pwd)

	a.repo.Create(&model)

	return model,nil
}

func (a *AdminService)Login(ctx iris.Context)(m map[string]interface{} ,err error){
	var adminInfo,admin models.Admin
	err=validator.ValidateInfo( ctx.ReadJSON(&adminInfo))
	if err!=nil{
		return
	}
	admin =a.repo.FindByName(adminInfo.Name)

	if reflect.DeepEqual(admin, models.Admin{}){
		err =errors.New("管理员不存在")
		return
	}

	if *admin.Status==0{
		err=errors.New("管理员被禁用")
		return
	}
	_,err=gbcrypt.ValidatePassWd(adminInfo.Pwd,admin.Pwd)
	if err!=nil{
		return
	}
  token,_:=middleware.GenerateToken(admin.ID,guard)
  admin.Pwd=""
   m= map[string]interface{}{
  	"token":string(token),
  	"user":admin,
   }
  return
}



func (a *AdminService)EditPwd(ctx iris.Context) error {


		new_pwd:=request.NewPassword{}
		pwd :=""
		err:=new_pwd.ValidateInfo(&new_pwd,ctx)
		if err!=nil{
			return err
		}

		m:=a.repo.FindById(application.Uid)
		_,err=gbcrypt.ValidatePassWd(new_pwd.OldPassword,m.Pwd)
		if err!=nil{
			return err
		}
		pwd,err=gbcrypt.GeneratePassWd(new_pwd.NewPassword)
		m.Pwd=pwd
		a.repo.Update(&m)
		return nil
}
